package com.gouqi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.common.*;
import com.gouqi.exception.BusinessException;
import com.gouqi.exception.ThrowUtils;
import com.gouqi.model.dto.app.AppAddRequest;
import com.gouqi.model.dto.app.AppEditRequest;
import com.gouqi.model.dto.app.AppQueryRequest;
import com.gouqi.model.dto.app.AppUpdateRequest;
import com.gouqi.model.entity.App;
import com.gouqi.model.entity.User;
import com.gouqi.model.enums.ReviewStatusEnum;
import com.gouqi.model.vo.AppVO;
import com.gouqi.service.AppService;
import com.gouqi.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 应用接口
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppService appService;

    // region 增删改查

    @ApiOperation("添加应用")
    @PostMapping("/add")
    public BaseResponse<Long> addApp(@RequestBody AppAddRequest appAddRequest, HttpServletRequest request) {
        long appId = appService.addApp(appAddRequest, request);
        return ResultUtils.success(appId);
    }

    @ApiOperation("删除应用")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteApp(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        appService.deleteApp(deleteRequest, request);
        return ResultUtils.success(true);
    }

    @ApiOperation("更新应用（仅管理员可用）")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateApp(@RequestBody AppUpdateRequest appUpdateRequest) {
        appService.updateApp(appUpdateRequest);
        return ResultUtils.success(true);
    }

    @ApiOperation("根据 id 获取应用（封装类）")
    @GetMapping("/get/vo")
    public BaseResponse<AppVO> getAppVOById(long id, HttpServletRequest request) {
        AppVO appVO = appService.getAppVO(id, request);
        return ResultUtils.success(appVO);
    }

    @ApiOperation("分页获取应用列表（仅管理员可用）")
    @PostMapping("/list/page")
    public BaseResponse<Page<App>> listAppByPage(@RequestBody AppQueryRequest appQueryRequest) {
        Page<App> appPage = appService.getAppPage(appQueryRequest);
        return ResultUtils.success(appPage);
    }

    @ApiOperation("分页获取应用列表（封装类）")
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<AppVO>> listAppVOByPage(@RequestBody AppQueryRequest appQueryRequest,
                                                     HttpServletRequest request) {
        Page<AppVO> appVOPage = appService.listAppVOByPage(appQueryRequest, request);
        return ResultUtils.success(appVOPage);
    }

    @ApiOperation("分页获取当前登录用户创建的应用列表")
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<AppVO>> listMyAppVOByPage(@RequestBody AppQueryRequest appQueryRequest,
                                                       HttpServletRequest request) {
        Page<AppVO> appVOPage = appService.listMyAppVOByPage(appQueryRequest, request);
        return ResultUtils.success(appVOPage);
    }

    @ApiOperation("编辑应用（给用户使用）")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editApp(@RequestBody AppEditRequest appEditRequest, HttpServletRequest request) {
        appService.editApp(appEditRequest, request);
        return ResultUtils.success(true);
    }

    // endregion

    @ApiOperation("审核应用")
    @PostMapping("/review")
    public BaseResponse<Boolean> doAppReview(@RequestBody ReviewRequest reviewRequest, HttpServletRequest request) {
        appService.doAppReview(reviewRequest, request);
        return ResultUtils.success(true);
    }

}
